//
//  HXLocaleJobTableViewCell.m
//  KD
//
//  Created by ifly on 16/4/21.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXLocaleJobTableViewCell.h"

@interface HXLocaleJobTableViewCell()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *groupLabel;
@property (weak, nonatomic) IBOutlet UIView *postDataBgView;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@property (nonatomic,assign) BOOL isTransform;
@property (nonatomic,assign) CGFloat angle;

@end

@implementation HXLocaleJobTableViewCell

-(void)awakeFromNib{
    [super awakeFromNib];
}

- (void)setData:(HXLocaleJobListModel *)data{
    _data = data;
    self.nameLabel.text = data.SiteName;
    self.dateLabel.text = data.CreateDate;
    self.groupLabel.text = data.GroupName;
    
    if (!data.isGet) {
        self.postDataBgView.hidden = YES;
    }else{
        self.postDataBgView.hidden = NO;
    }
}

- (IBAction)postDataBtnAct:(id)sender {

    self.imgView.image = [UIImage imageNamed:@"65-2"];

    if (!self.isTransform) {
        
        self.isTransform = YES;
        [self startAnimation];
        
        NSArray * taskIdArr = [HXSendPicModel MR_findByAttribute:@"taskId" withValue:self.data.ID];
        HXSendPicModel * picModel = taskIdArr[0];
        NSData * picData = ((HXSendPicContentModel *)picModel.contentModel).contentData;
        NSDictionary * picDic = [NSJSONSerialization JSONObjectWithData:picData options:NSJSONReadingAllowFragments error:nil];

        if (picDic.count > 0) {
            
            NSDictionary * modelDic = [picDic objectForKey:@"model"];
            NSString * nowNetworkTime = [modelDic objectForKey:@"SubmitDate"];
            
            if (nowNetworkTime.length <= 0 || nowNetworkTime == nil) {
                
                nowNetworkTime = [GlobalUtil nowTimeDateFormatterWithNowTime:[GlobalUtil getNowPhoneTime]];
            }
            
            dispatch_queue_t getNetwork = dispatch_queue_create("getNetwork", NULL);
            
            dispatch_sync(getNetwork, ^{
                
                //1.修改小组人员、任务内容（一个工作日内）
                [NetworkAPI changePresonTaskContentWithTaskID:picModel.taskId
                                                     SiteName:[modelDic objectForKey:@"Task_SiteWorkContent"]
                                                    GroupName:[modelDic objectForKey:@"Task_GroupName"]
                                                   SubmitDate:nowNetworkTime
                                                      Success:^(HXBaseModel *result) {
                                                          if ([result.code isEqualToString:@"200"]) {
                                                              //[SVProgressHUD showSuccessWithStatus:@"修改成功"];
                                                          }
                                                      } failure:^(NSError *err) {
                                                          
                                                      }];
            });

            NSDictionary * dataDic = [picDic objectForKey:@"dict"];
            [self postMenKouAndPunishWithDic:dataDic
                               withTaskIdArr:taskIdArr];
        }
    }
}

/**上传 门口照片 及 处罚取证的照片*/
-(void)postMenKouAndPunishWithDic:(NSDictionary *)vcOneDic
                    withTaskIdArr:(NSArray *)taskIdArr{
    
    [NetworkAPI newCreateAddWithDic:vcOneDic
                            success:^(HXSendInformationModel *result) {
                                if ([result.code integerValue] == 200) {
                                    self.data.isGet = NO;
                                    self.returnLocaleJobListModel(self.data);
                                    
                                    //删除数据
                                    for (HXSendPicModel * model in taskIdArr) {
                                        BOOL isDelete = [model MR_deleteEntity];
                                        if (isDelete) {
                                            [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
                                        }
                                    }
                                    
                                    self.isTransform = NO;
                                    self.imgView.transform = CGAffineTransformIdentity;
                                    self.imgView.image = [UIImage imageNamed:@"65"];
                                    
                                }else{
                                    self.isTransform = NO;
                                    self.imgView.transform = CGAffineTransformIdentity;
                                    self.imgView.image = [UIImage imageNamed:@"65"];

                                }
                            }
                            failure:^(NSError * err) {
                                self.isTransform = NO;
                                self.imgView.transform = CGAffineTransformIdentity;
                                self.imgView.image = [UIImage imageNamed:@"65"];

                            }];
    
}



//第二种旋转方式(使用Block)

- (void)startAnimation{
    
    CGAffineTransform endAngle = CGAffineTransformMakeRotation(_angle * (M_PI /180.0f));
    
    [UIView animateWithDuration:0.02 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        
        self.imgView.transform = endAngle;

        
    } completion:^(BOOL finished) {
        
        _angle += 10;
        
        if (self.isTransform) {
            [self startAnimation];
        }
    }];
}


@end
